package com.visikard.ui.activity;


import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.ui.R;
import com.visikard.ui.fragment.MyDatePickerDialog;
import com.visikard.ui.fragment.MyDatePickerDialog.SelectDateCallback;
import com.visikard.utility.Utils;

public class CouponInformationActivity extends BaseFragmentActivity implements SelectDateCallback {
	private CouponInformationActivity me=this;
	private EditText mEditText_cpname,mEditText_cppercent,mEditText_cpmax,mEditText_cpwriteup,mEditText_location;
	private TextView mTextView_start_month,mTextView_start_day,mTextView_start_year;
	private TextView mTextView_end_month,mTextView_end_day,mTextView_end_year;
	private Button mButton_next,mButton_back;
	final int REQUEST_CODE_COUPON_INFO=1;
	private void initControls(){
		mEditText_cpname=(EditText) me.findViewById(R.id.coupon_name);
		mEditText_cppercent=(EditText) me.findViewById(R.id.coupon_percent);
		mEditText_cpmax=(EditText) me.findViewById(R.id.coupon_maxoff);
		mEditText_cpwriteup=(EditText) me.findViewById(R.id.write_up_to);
		
		mEditText_location=(EditText) me.findViewById(R.id.participating_locations);
		
		mTextView_start_month=(TextView) me.findViewById(R.id.start_date_month);
		mTextView_start_day=(TextView) me.findViewById(R.id.start_date_day);
		mTextView_start_year=(TextView) me.findViewById(R.id.start_date_year);
		
		mTextView_end_month=(TextView) me.findViewById(R.id.end_date_month);
		mTextView_end_day=(TextView) me.findViewById(R.id.end_date_day);
		mTextView_end_year=(TextView) me.findViewById(R.id.end_date_year);
		
		mButton_next=(Button) me.findViewById(R.id.btn_next);
		mButton_back=(Button) me.findViewById(R.id.btn_back);
		
		mEditText_location.setOnClickListener(new MyOnClickListenner());
		
		mTextView_start_month.setOnClickListener(new MyOnClickListenner());
		mTextView_start_day.setOnClickListener(new MyOnClickListenner());
		mTextView_start_year.setOnClickListener(new MyOnClickListenner());
		
		mTextView_end_month.setOnClickListener(new MyOnClickListenner());
		mTextView_end_day.setOnClickListener(new MyOnClickListenner());
		mTextView_end_year.setOnClickListener(new MyOnClickListenner());
		
		mButton_next.setOnClickListener(new MyOnClickListenner());
		mButton_back.setOnClickListener(new MyOnClickListenner());
	}
	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		setContentView(R.layout.coupon_information_activity);
		initControls();
	}
	private class MyOnClickListenner implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v==mEditText_location){
				
			}else if(v==mTextView_start_day||v==mTextView_start_month||v==mTextView_start_year){
				MyDatePickerDialog datepicker = new MyDatePickerDialog(CouponInformationActivity.this,1);
				datepicker.show(getSupportFragmentManager(), "datepickerdialog");
			}else if(v==mTextView_end_day||v==mTextView_end_month||v==mTextView_end_year){
				MyDatePickerDialog datepicker = new MyDatePickerDialog(CouponInformationActivity.this,2);
				datepicker.show(getSupportFragmentManager(), "datepickerdialog");
			}else if(v==mButton_back){
				finish();
			}else if(v==mButton_next){
				if(validateData()){
					Intent intent=new Intent(CouponInformationActivity.this,CouponPreviewActivity.class);
					me.startActivityForResult(intent, REQUEST_CODE_COUPON_INFO);
				}
				
			}
		}
		
	}
	@Override
	public void onSelectDateCallback(int day, int month, int year,int StartOrEnd) {
		// TODO Auto-generated method stub
		if(StartOrEnd==1){
			mTextView_start_day.setText(String.valueOf(day));
			mTextView_start_month.setText(String.valueOf(month));
			mTextView_start_year.setText(String.valueOf(year));
		}if(StartOrEnd==2){
			mTextView_end_day.setText(String.valueOf(day));
			mTextView_end_month.setText(String.valueOf(month));
			mTextView_end_year.setText(String.valueOf(year));
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_CODE_COUPON_INFO) {
				
			}
		}
	}
	private boolean validateData(){
		boolean result = false;
		if(mEditText_cpname.getText().toString().trim().equals("")){
			Utils.showToast(me, String.format(me.getString(R.string.alert_empty), mEditText_cpname.getHint().toString()), 0);
			
		}

		else if (mTextView_start_month.getText().toString().equals(me.getString(R.string.txt_month))){
			Utils.showToast(me, String.format(me.getString(R.string.alert_empty), "Start Date"), 0);
		}
		else if (mTextView_end_month.getText().toString().equals(me.getString(R.string.txt_month))){
			Utils.showToast(me, String.format(me.getString(R.string.alert_empty), "End Date"), 0);
		}
		
		else{
			Calendar calendar =Calendar.getInstance();
			calendar.set(Calendar.YEAR, Integer.parseInt(mTextView_start_year.getText().toString()));
			calendar.set(Calendar.MONTH, Integer.parseInt(mTextView_start_month.getText().toString()));
			calendar.set(Calendar.DATE, Integer.parseInt(mTextView_start_day.getText().toString()));
			Date start = calendar.getTime();
			
			calendar.set(Calendar.YEAR, Integer.parseInt(mTextView_end_year.getText().toString()));
			calendar.set(Calendar.MONTH, Integer.parseInt(mTextView_end_month.getText().toString()));
			calendar.set(Calendar.DATE, Integer.parseInt(mTextView_end_day.getText().toString()));
			Date end = calendar.getTime();
			if(end.compareTo(start) < 0){
				Utils.showToast(me, me.getString(R.string.alert_enddate_startdate), 0);
				
			}else{
				return true;
			}
			
		}
		
		return result;
	}
	public void validDate(){
		
	}
}
